<?php

/**
 * This is the model class for table "{{people}}".
 *
 * The followings are the available columns in table '{{people}}':
 * @property string $id
 * @property string $image
 * @property string $user_name
 * @property string $user_phone
 * @property string $openid
 * @property string $key_sn
 * @property integer $role
 * @property string $code_num
 * @property integer $sex
 * @property integer $birth_month
 * @property integer $birth_day
 * @property integer $start_time
 * @property integer $gid
 * @property integer $cid
 * @property integer $relation_id
 * @property string $is_relation
 * @property string $is_subject
 * @property string $is_login
 * @property string $passwd
 * @property integer $point
 * @property string $zimu
 */
class People extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{people}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('role, sex, birth_month, birth_day, start_time, gid, cid, relation_id, point', 'numerical', 'integerOnly'=>true),
			array('image', 'length', 'max'=>255),
			array('user_name, user_phone, code_num, is_relation, is_subject, is_login', 'length', 'max'=>20),
			array('openid, passwd', 'length', 'max'=>40),
			array('key_sn', 'length', 'max'=>10),
			array('zimu', 'length', 'max'=>3),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, image, user_name, user_phone, openid, key_sn, role, code_num, sex, birth_month, birth_day, start_time, gid, cid, relation_id, is_relation, is_subject, is_login, passwd, point, zimu', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
        return array(
            'ga'=>array(self::BELONGS_TO, 'Grade', 'gid'),
            'ca'=>array(self::BELONGS_TO, 'Classes', 'cid')
        );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'image' => 'Image',
			'user_name' => 'User Name',
			'user_phone' => 'User Phone',
			'openid' => 'Openid',
			'key_sn' => 'Key Sn',
			'role' => 'Role',
			'code_num' => 'Code Num',
			'sex' => 'Sex',
			'birth_month' => 'Birth Month',
			'birth_day' => 'Birth Day',
			'start_time' => 'Start Time',
			'gid' => 'Gid',
			'cid' => 'Cid',
			'relation_id' => 'Relation',
			'is_relation' => 'Is Relation',
			'is_subject' => 'Is Subject',
			'is_login' => 'Is Login',
			'passwd' => 'Passwd',
			'point' => 'Point',
			'zimu' => 'Zimu',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_phone',$this->user_phone,true);
		$criteria->compare('openid',$this->openid,true);
		$criteria->compare('key_sn',$this->key_sn,true);
		$criteria->compare('role',$this->role);
		$criteria->compare('code_num',$this->code_num,true);
		$criteria->compare('sex',$this->sex);
		$criteria->compare('birth_month',$this->birth_month);
		$criteria->compare('birth_day',$this->birth_day);
		$criteria->compare('start_time',$this->start_time);
		$criteria->compare('gid',$this->gid);
		$criteria->compare('cid',$this->cid);
		$criteria->compare('relation_id',$this->relation_id);
		$criteria->compare('is_relation',$this->is_relation,true);
		$criteria->compare('is_subject',$this->is_subject,true);
		$criteria->compare('is_login',$this->is_login,true);
		$criteria->compare('passwd',$this->passwd,true);
		$criteria->compare('point',$this->point);
		$criteria->compare('zimu',$this->zimu,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return People the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getPaihang($type,$user_info){
        $db = Yii::app()->db->createCommand();
        if($type==1){//班级排行
            $cid = $user_info->cid;

            $haipan_sql = "SELECT M.pm
                        FROM (SELECT A.*,@rank:=@rank+1 as pm
                        FROM ( SELECT id,point FROM
                        {{people}} WHERE cid='$cid' AND role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
                        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
            $connection = Yii::app()->db;
            $command = $connection->createCommand($haipan_sql);
            $result = $command->queryRow();
            $paihang = $result['pm'];
        }elseif($type==2){
            $gid = $user_info->gid;
            $haipan_sql = "SELECT M.pm
                        FROM (SELECT A.*,@rank:=@rank+1 as pm
                        FROM ( SELECT id,point FROM
                        {{people}} WHERE gid='$gid' AND role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
                        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
            $connection = Yii::app()->db;
            $command = $connection->createCommand($haipan_sql);
            $result = $command->queryRow();
            $paihang = $result['pm'];
        }else{


            $haipan_sql = "SELECT M.pm
                        FROM (SELECT A.*,@rank:=@rank+1 as pm
                        FROM ( SELECT id,point FROM
                        {{people}} WHERE role=1 ORDER BY point DESC ) A ,(SELECT @rank:=0) B ) M
                        WHERE M.`id`='$user_info->id' ORDER BY M.pm ASC";
            $connection = Yii::app()->db;
            $command = $connection->createCommand($haipan_sql);
            $result = $command->queryRow();
            $paihang = $result['pm'];
        }

        return $paihang;
    }
    public function getListPoint($type,$user_info){
        $db = Yii::app()->db->createCommand();
        if($type==1){//班级排行
            $info = $db
                ->select('*')
                ->from('{{people}}')
                ->where('role=:role and cid=:cid and point !=:point',array(':cid'=>$user_info->cid,':role'=>1,':point'=>0))
                ->order('point desc')
                ->limit(100)
                ->queryAll();
        }elseif($type==2){
            $info = $db
                ->select('*')
                ->from('{{people}}')
                ->where('role=:role and gid=:gid and point !=:point',array(':gid'=>$user_info->gid,':role'=>1,':point'=>0))
                ->order('point desc')
                ->limit(100)
                ->queryAll();
        }else{
            $info = $db
                ->select('*')
                ->from('{{people}}')
                ->where('role=:role and point !=:point',array(':role'=>1,':point'=>0))
                ->order('point desc')
                ->limit(100)
                ->queryAll();
        }

        return $info;
    }
}
